<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="技术,分享,生活">
    <meta name="description" content="这里是anlondon,欢迎留言交流">
    <meta name="author" content="anlondon">
    
    <title>
        
            微信小程序客服配置 |
        
        一座浮岛
    </title>
    
<link rel="stylesheet" href="/css/style.css">

    <link rel="shortcut icon" href="/images/logo_my.png">
    
<link rel="stylesheet" href="/fontawesome/css/fontawesome.min.css">

    
<link rel="stylesheet" href="/fontawesome/css/regular.min.css">

    
<link rel="stylesheet" href="/fontawesome/css/solid.min.css">

    
<link rel="stylesheet" href="/fontawesome/css/brands.min.css">

    <script id="hexo-configurations">
    let KEEP = window.KEEP || {};
    KEEP.hexo_config = {"hostname":"example.com","root":"/","language":"en","path":"search.json"};
    KEEP.theme_config = {"toc":{"enable":true,"number":true,"expand_all":true,"init_open":true},"style":{"primary_color":"#557ea2","logo":"/images/logo_my.png","favicon":"/images/logo_my.png","avatar":"/images/avatar_my.svg","font_size":null,"font_family":"STKaiti","hover":{"shadow":true,"scale":true},"first_screen":{"enable":true,"header_transparent":false,"background_img":"https://api.likepoems.com/img/nature","description":null,"font_color":null,"hitokoto":true},"scroll":{"progress_bar":false,"percent":false}},"local_search":{"enable":true,"preload":true},"code_copy":{},"code_block":{"tools":{"enable":true,"style":"default"},"highlight_theme":"obsidian"},"side_tools":{},"pjax":{"enable":true},"lazyload":{"enable":true},"comment":{"enable":true,"use":"valine","valine":{"appid":"v7SvjMu6zFX3LPmiYGos39qx-gzGzoHsz","appkey":"MlLOMElKkabjL3SHiqXhFWAW","placeholder":null},"gitalk":{"github_id":null,"github_admins":null,"repository":null,"client_id":null,"client_secret":null},"twikoo":{"env_id":null,"region":null,"version":"1.6.7"},"waline":{"server_url":null,"reaction":false,"version":2}},"post":{"author_label":{"enable":true,"auto":true,"custom_label_list":["Trainee","Engineer","Architect"]},"word_count":{"enable":true,"wordcount":true,"min2read":true},"img_align":"left","copyright_info":false},"version":"3.5.2"};
    KEEP.language_ago = {"second":"%s seconds ago","minute":"%s minutes ago","hour":"%s hours ago","day":"%s days ago","week":"%s weeks ago","month":"%s months ago","year":"%s years ago"};
    KEEP.language_code_block = {"copy":"Copy code","copied":"Copied","fold":"Fold code block","folded":"Folded"};
  </script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
<!-- anlondon 加的 start -->
<div class="background-my"></div>
<!-- anlondon 加的 end-->
<div class="progress-bar-container">
    

    
        <span class="pjax-progress-bar"></span>
        <i class="pjax-progress-icon fas fa-circle-notch fa-spin"></i>
    
</div>


<main class="page-container">

    

    <div class="page-main-content">

        <div class="page-main-content-top">
            
<header class="header-wrapper">

    <div class="header-content">
        <div class="left">
            
                <a class="logo-image" href="/">
                    <img src="/images/logo_my.png">
                </a>
            
            <a class="logo-title" href="/">
               一座浮岛
            </a>
        </div>

        <div class="right">
            <div class="pc">
                <ul class="menu-list">
                    
                        <li class="menu-item">
                            <a class=""
                               href="/"
                            >
                                首页
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/archives"
                            >
                                时间流
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/categories"
                            >
                                归档
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/links"
                            >
                                LINKS
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/about"
                            >
                                关于
                            </a>
                        </li>
                    
                    
                        <li class="menu-item search search-popup-trigger">
                            <i class="fas fa-search"></i>
                        </li>
                    
                </ul>
            </div>
            <div class="mobile">
                
                    <div class="icon-item search search-popup-trigger"><i class="fas fa-search"></i></div>
                
                <div class="icon-item menu-bar">
                    <div class="menu-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-drawer">
        <ul class="drawer-menu-list">
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/">首页</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/archives">时间流</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/categories">归档</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/links">LINKS</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/about">关于</a>
                </li>
            
        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="page-main-content-middle">

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="post-page-container">
        <div class="article-content-container">

            <div class="article-title">
                <span class="title-hover-animation">微信小程序客服配置</span>
            </div>

            
                <div class="article-header">
                    <div class="avatar">
                        <img src="/images/avatar_my.svg">
                    </div>
                    <div class="info">
                        <div class="author">
                            <span class="name">anlondon</span>
                            
                                <span class="author-label">Lv6</span>
                            
                        </div>
                        <div class="meta-info">
                            
<div class="article-meta-info">
    <span class="article-date article-meta-item">
        
            <i class="fa-regular fa-calendar-plus"></i>&nbsp;
        
        <span class="pc">2021-07-06 17:41:00</span>
        <span class="mobile">2021-07-06 17:41</span>
    </span>
    
        <span class="article-update-date article-meta-item">
        <i class="fas fa-file-pen"></i>&nbsp;
        <span class="pc">2021-08-03 10:39:43</span>
    </span>
    
    
        <span class="article-categories article-meta-item">
            <i class="fas fa-folder"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/categories/%E5%BE%AE%E4%BF%A1/">微信</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fas fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/tags/%E5%BE%AE%E4%BF%A1/">微信</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
    
    
        <span class="article-pv article-meta-item">
            <i class="fas fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                        </div>
                    </div>
                </div>
            

            <div class="article-content keep-markdown-body">
                <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/hint.css/2.4.1/hint.min.css"><h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p><strong>#</strong> 建议各位先按底部的参考资料顺序查看，以便充分了解步骤以及设置要点</p>
<p>配置：</p>
<ul>
<li>php7.4</li>
<li>tp5</li>
<li>easyWechat</li>
</ul>
<h3 id="部署"><a href="#部署" class="headerlink" title="部署"></a>部署</h3><h5 id="一，验证token"><a href="#一，验证token" class="headerlink" title="一，验证token"></a>一，验证token</h5><p>根据<a class="link"   target="_blank" rel="noopener" href="https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/message-push.html" >消息推送|token 验证<i class="fas fa-external-link-alt"></i></a>的说明，在配置消息推送时，需要在对应的请求里添加操作<br><img  
                     lazyload
                     alt="image"
                     data-src="https://res.wx.qq.com/wxdoc/dist/assets/img/callback_help.ae01307b.png"
                     
                ></p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$signature</span> = <span class="variable">$_GET</span>[<span class="string">&quot;signature&quot;</span>];</span><br><span class="line">        <span class="variable">$timestamp</span> = <span class="variable">$_GET</span>[<span class="string">&quot;timestamp&quot;</span>];</span><br><span class="line">        <span class="variable">$nonce</span> = <span class="variable">$_GET</span>[<span class="string">&quot;nonce&quot;</span>];</span><br><span class="line"></span><br><span class="line">        <span class="variable">$tmpArr</span> = <span class="keyword">array</span>(<span class="keyword">$this</span>-&gt;Token, <span class="variable">$timestamp</span>, <span class="variable">$nonce</span>);</span><br><span class="line">        sort(<span class="variable">$tmpArr</span>, SORT_STRING);</span><br><span class="line">        <span class="variable">$tmpStr</span> = implode( <span class="variable">$tmpArr</span> );</span><br><span class="line">        <span class="variable">$tmpStr</span> = sha1( <span class="variable">$tmpStr</span> );</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(<span class="variable">$tmpStr</span> == <span class="variable">$signature</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br></pre></td></tr></table></figure>

<p>后端完整代码</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title">addons</span>\<span class="title">shopro</span>\<span class="title">controller</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">addons</span>\<span class="title">shopro</span>\<span class="title">library</span>\<span class="title">Wechat</span> <span class="title">as</span> <span class="title">WechatLibrary</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">think</span>\<span class="title">Request</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 微信小程序客服接口</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">WechatKefu</span> <span class="keyword">extends</span> <span class="title">Base</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">protected</span> <span class="variable">$noNeedLogin</span> = [<span class="string">&#x27;*&#x27;</span>];</span><br><span class="line">    <span class="keyword">protected</span> <span class="variable">$noNeedRight</span> = [<span class="string">&#x27;*&#x27;</span>];</span><br><span class="line">    <span class="keyword">protected</span> <span class="variable">$app</span> = <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">protected</span> <span class="variable">$Token</span> = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">protected</span> <span class="variable">$EncodingAESKey</span> = <span class="literal">null</span>;</span><br><span class="line">    <span class="comment">// https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=wx****&amp;secret=&#x27;***&#x27;; //替换成自己的小程序id和secret</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="variable">$get_accessToken_url</span> = <span class="string">&#x27;https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&#x27;</span>; <span class="comment">// 获取accessToken</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="variable">$send_message_url</span> = <span class="string">&#x27;https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=&#x27;</span>; <span class="comment">// 发送消息给用户，用于用户联系客服时自动回复，不让微信服务器自动转发</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">Request <span class="variable">$request</span> = <span class="literal">null</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="built_in">parent</span>::__construct(<span class="variable">$request</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">$this</span>-&gt;Token = config(<span class="string">&#x27;site.wechatKefuToken&#x27;</span>);</span><br><span class="line">        <span class="keyword">$this</span>-&gt;EncodingAESKey = config(<span class="string">&#x27;site.wechatKefuEncodingAESKey&#x27;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="variable">$wechat</span> = <span class="keyword">new</span> WechatLibrary(<span class="string">&#x27;wxMiniProgram&#x27;</span>);</span><br><span class="line">        <span class="keyword">$this</span>-&gt;app = <span class="variable">$wechat</span>-&gt;getApp();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 微信小程序客服对接、处理消息回复</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">isset</span>(<span class="variable">$_GET</span>[<span class="string">&#x27;echostr&#x27;</span>]))</span><br><span class="line">            <span class="keyword">$this</span>-&gt;valid();</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">            <span class="keyword">$this</span>-&gt;responseMsg();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 验证token时使用 配置小程序后台-&gt;开发-&gt;开发管理-&gt;开发设置-&gt;消息推送</span></span><br><span class="line">    <span class="keyword">private</span> <span class="function"><span class="keyword">function</span> <span class="title">valid</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="variable">$echoStr</span> = <span class="variable">$_GET</span>[<span class="string">&quot;echostr&quot;</span>];</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">$this</span>-&gt;checkSignature())&#123;</span><br><span class="line">            header(<span class="string">&#x27;content-type:text&#x27;</span>);</span><br><span class="line">            <span class="keyword">echo</span> <span class="variable">$echoStr</span>; <span class="comment">// 成功</span></span><br><span class="line">            <span class="keyword">exit</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="keyword">exit</span>(<span class="string">&#x27;fail! &#x27;</span>.<span class="variable">$echoStr</span>.<span class="string">&#x27;+++&#x27;</span>.<span class="keyword">$this</span>-&gt;Token); <span class="comment">// 失败</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 配置小程序-&gt;消息推送-&gt;token验证时启用</span></span><br><span class="line">    <span class="keyword">private</span> <span class="function"><span class="keyword">function</span> <span class="title">checkSignature</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="variable">$signature</span> = <span class="variable">$_GET</span>[<span class="string">&quot;signature&quot;</span>];</span><br><span class="line">        <span class="variable">$timestamp</span> = <span class="variable">$_GET</span>[<span class="string">&quot;timestamp&quot;</span>];</span><br><span class="line">        <span class="variable">$nonce</span> = <span class="variable">$_GET</span>[<span class="string">&quot;nonce&quot;</span>];</span><br><span class="line"></span><br><span class="line">        <span class="variable">$tmpArr</span> = <span class="keyword">array</span>(<span class="keyword">$this</span>-&gt;Token, <span class="variable">$timestamp</span>, <span class="variable">$nonce</span>);</span><br><span class="line">        sort(<span class="variable">$tmpArr</span>, SORT_STRING);</span><br><span class="line">        <span class="variable">$tmpStr</span> = implode( <span class="variable">$tmpArr</span> );</span><br><span class="line">        <span class="variable">$tmpStr</span> = sha1( <span class="variable">$tmpStr</span> );</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(<span class="variable">$tmpStr</span> == <span class="variable">$signature</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 发送消息</span></span><br><span class="line">    <span class="keyword">private</span> <span class="function"><span class="keyword">function</span> <span class="title">responseMsg</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="variable">$postStr</span> = file_get_contents(<span class="string">&#x27;php://input&#x27;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">empty</span>(<span class="variable">$postStr</span>) &amp;&amp; !is_string(<span class="variable">$postStr</span>))&#123;</span><br><span class="line">            <span class="keyword">echo</span> <span class="string">&quot;&quot;</span>;</span><br><span class="line">            <span class="keyword">exit</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//禁止引用外部xml实体</span></span><br><span class="line">        <span class="comment">//libxml_disable_entity_loader(true);</span></span><br><span class="line">        <span class="comment">//$postObj = simplexml_load_string($postStr, &#x27;SimpleXMLElement&#x27;, LIBXML_NOCDATA);</span></span><br><span class="line">        <span class="variable">$postArr</span> =json_decode(<span class="variable">$postStr</span>,<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">        <span class="variable">$fromUsername</span> =<span class="variable">$postArr</span>[<span class="string">&#x27;FromUserName&#x27;</span>]; <span class="comment">//发送者openid</span></span><br><span class="line">        <span class="variable">$toUserName</span> =<span class="variable">$postArr</span>[<span class="string">&#x27;ToUserName&#x27;</span>]; <span class="comment">//小程序id</span></span><br><span class="line">        <span class="keyword">if</span>(!<span class="keyword">empty</span>(<span class="variable">$postArr</span>[<span class="string">&#x27;MsgType&#x27;</span>]) &amp;&amp;<span class="variable">$postArr</span>[<span class="string">&#x27;MsgType&#x27;</span>] ==<span class="string">&#x27;text&#x27;</span>)&#123;<span class="comment">//文本消息</span></span><br><span class="line">            <span class="variable">$textTpl</span> =<span class="keyword">array</span>(</span><br><span class="line">                <span class="string">&quot;ToUserName&quot;</span>=&gt;<span class="variable">$fromUsername</span>,</span><br><span class="line">                <span class="string">&quot;FromUserName&quot;</span>=&gt;<span class="variable">$toUserName</span>,</span><br><span class="line">                <span class="string">&quot;CreateTime&quot;</span>=&gt;<span class="variable">$_SERVER</span>[<span class="string">&#x27;REQUEST_TIME&#x27;</span>],</span><br><span class="line">                <span class="string">&quot;MsgType&quot;</span>=&gt;<span class="string">&quot;transfer_customer_service&quot;</span>,</span><br><span class="line">            );</span><br><span class="line"></span><br><span class="line">            <span class="keyword">exit</span>(json_encode(<span class="variable">$textTpl</span>));</span><br><span class="line">        &#125;<span class="keyword">elseif</span>(!<span class="keyword">empty</span>(<span class="variable">$postArr</span>[<span class="string">&#x27;MsgType&#x27;</span>]) &amp;&amp;<span class="variable">$postArr</span>[<span class="string">&#x27;MsgType&#x27;</span>] ==<span class="string">&#x27;image&#x27;</span>)&#123;<span class="comment">//图文消息</span></span><br><span class="line">            <span class="variable">$textTpl</span> =<span class="keyword">array</span>(</span><br><span class="line">                <span class="string">&quot;ToUserName&quot;</span>=&gt;<span class="variable">$fromUsername</span>,</span><br><span class="line">                <span class="string">&quot;FromUserName&quot;</span>=&gt;<span class="variable">$toUserName</span>,</span><br><span class="line">                <span class="string">&quot;CreateTime&quot;</span>=&gt;<span class="variable">$_SERVER</span>[<span class="string">&#x27;REQUEST_TIME&#x27;</span>],</span><br><span class="line">                <span class="string">&quot;MsgType&quot;</span>=&gt;<span class="string">&quot;transfer_customer_service&quot;</span>,</span><br><span class="line">            );</span><br><span class="line">            <span class="keyword">exit</span>(json_encode(<span class="variable">$textTpl</span>));</span><br><span class="line">        &#125;<span class="keyword">elseif</span>(<span class="variable">$postArr</span>[<span class="string">&#x27;MsgType&#x27;</span>] ==<span class="string">&#x27;event&#x27;</span> &amp;&amp;<span class="variable">$postArr</span>[<span class="string">&#x27;Event&#x27;</span>]==<span class="string">&#x27;user_enter_tempsession&#x27;</span>)&#123;<span class="comment">//进入客服动作</span></span><br><span class="line">            <span class="variable">$content</span> =<span class="string">&#x27;您好，有什么能帮助你?&#x27;</span>;</span><br><span class="line">            <span class="variable">$data</span>=<span class="keyword">array</span>(</span><br><span class="line">                <span class="string">&quot;touser&quot;</span>=&gt;<span class="variable">$fromUsername</span>,</span><br><span class="line">                <span class="string">&quot;msgtype&quot;</span>=&gt;<span class="string">&quot;text&quot;</span>,</span><br><span class="line">                <span class="string">&quot;text&quot;</span>=&gt;<span class="keyword">array</span>(<span class="string">&quot;content&quot;</span>=&gt;<span class="variable">$content</span>)</span><br><span class="line">            );</span><br><span class="line">            <span class="variable">$json</span> =json_encode(<span class="variable">$data</span>,JSON_UNESCAPED_UNICODE);</span><br><span class="line"></span><br><span class="line">            <span class="keyword">$this</span>-&gt;requestAPI(<span class="variable">$json</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span></span><br><span class="line">            <span class="keyword">exit</span>(<span class="string">&#x27;Error,未知类型会话&#x27;</span>);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 发送消息到指定</span></span><br><span class="line">    <span class="keyword">private</span> <span class="function"><span class="keyword">function</span> <span class="title">requestAPI</span>(<span class="params"><span class="variable">$json</span></span>)</span>&#123;</span><br><span class="line">        <span class="variable">$access_token</span> = <span class="keyword">$this</span>-&gt;get_accessToken();</span><br><span class="line">        <span class="keyword">$this</span>-&gt;send_message_url = <span class="keyword">$this</span>-&gt;send_message_url.<span class="variable">$access_token</span>; <span class="comment">// 发送消息  POST发送https请求客服接口api</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//以&#x27;json&#x27;格式发送post的https请求</span></span><br><span class="line">        <span class="variable">$curl</span> = curl_init();</span><br><span class="line">        curl_setopt(<span class="variable">$curl</span>, CURLOPT_URL, <span class="keyword">$this</span>-&gt;send_message_url);</span><br><span class="line">        curl_setopt(<span class="variable">$curl</span>, CURLOPT_POST, <span class="number">1</span>); <span class="comment">// 发送一个常规的Post请求</span></span><br><span class="line">        curl_setopt(<span class="variable">$curl</span>, CURLOPT_SSL_VERIFYPEER, <span class="literal">FALSE</span>);</span><br><span class="line">        curl_setopt(<span class="variable">$curl</span>, CURLOPT_SSL_VERIFYHOST, <span class="literal">FALSE</span>);</span><br><span class="line">        <span class="keyword">if</span> (!<span class="keyword">empty</span>(<span class="variable">$json</span>))</span><br><span class="line">            curl_setopt(<span class="variable">$curl</span>, CURLOPT_POSTFIELDS,<span class="variable">$json</span>);</span><br><span class="line"></span><br><span class="line">        curl_setopt(<span class="variable">$curl</span>, CURLOPT_RETURNTRANSFER, <span class="number">1</span>);</span><br><span class="line">        <span class="comment">//curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );</span></span><br><span class="line">        <span class="variable">$output</span> = curl_exec(<span class="variable">$curl</span>);</span><br><span class="line">        <span class="keyword">if</span> (curl_errno(<span class="variable">$curl</span>))</span><br><span class="line">            <span class="keyword">echo</span> <span class="string">&#x27;Errno&#x27;</span>.curl_error(<span class="variable">$curl</span>);<span class="comment">//捕抓异常</span></span><br><span class="line"></span><br><span class="line">        curl_close(<span class="variable">$curl</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(<span class="variable">$output</span> == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">echo</span> <span class="string">&#x27;success&#x27;</span>;</span><br><span class="line">            <span class="keyword">exit</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 调用微信api，获取access_token，有效期7200s</span></span><br><span class="line">    <span class="keyword">private</span> <span class="function"><span class="keyword">function</span> <span class="title">get_accessToken</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="variable">$url</span> = <span class="keyword">$this</span>-&gt;get_accessToken_url.<span class="string">&#x27;&amp;appid=&#x27;</span>.<span class="keyword">$this</span>-&gt;app-&gt;config[<span class="string">&#x27;app_id&#x27;</span>].<span class="string">&#x27;&amp;secret=&#x27;</span>.<span class="keyword">$this</span>-&gt;app-&gt;config[<span class="string">&#x27;secret&#x27;</span>]; <span class="comment">//替换成自己的小程序id和secret</span></span><br><span class="line">        @<span class="variable">$weixin</span> = file_get_contents(<span class="variable">$url</span>);</span><br><span class="line">        @<span class="variable">$jsondecode</span> = json_decode(<span class="variable">$weixin</span>);</span><br><span class="line">        @<span class="variable">$array</span> = get_object_vars(<span class="variable">$jsondecode</span>);</span><br><span class="line">        <span class="variable">$token</span> = <span class="variable">$array</span>[<span class="string">&#x27;access_token&#x27;</span>];</span><br><span class="line">        <span class="keyword">return</span> <span class="variable">$token</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<hr>
<p>参考资料：</p>
<ul>
<li><a class="link"   target="_blank" rel="noopener" href="https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/message-push.html" >消息推送|token 验证(官方)<i class="fas fa-external-link-alt"></i></a></li>
<li><a class="link"   target="_blank" rel="noopener" href="https://developers.weixin.qq.com/miniprogram/dev/component/button.html" >button|小程序按钮(官方)<i class="fas fa-external-link-alt"></i></a></li>
<li><a class="link"   target="_blank" rel="noopener" href="https://developers.weixin.qq.com/miniprogram/introduction/custom.html#%E7%BD%91%E9%A1%B5%E7%89%88%E5%AE%A2%E6%9C%8D%E5%B7%A5%E5%85%B7" >客服消息使用指南|整体介绍(官方)<i class="fas fa-external-link-alt"></i></a></li>
</ul>
<ul>
<li><a class="link"   target="_blank" rel="noopener" href="https://developers.weixin.qq.com/doc/offiaccount/Customer_Service/Forwarding_of_messages_to_service_center.html" >客服消息|开发文档(官方)<i class="fas fa-external-link-alt"></i></a></li>
<li><a class="link"   target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_42342572/article/details/80506303" >仅回复模板消息，非在线聊天|csdn 微信小程序【客服消息】功能开发<i class="fas fa-external-link-alt"></i></a></li>
<li><a class="link"   target="_blank" rel="noopener" href="https://www.jianshu.com/p/d3fe269ed931" >前端button设置|简书 微信小程序之在线客服（即时聊天）<i class="fas fa-external-link-alt"></i></a></li>
</ul>

            </div>

            

            
                <ul class="post-tags-box">
                    
                        <li class="tag-item">
                            <a href="/tags/%E5%BE%AE%E4%BF%A1/">#微信</a>&nbsp;
                        </li>
                    
                </ul>
            

            
                <div class="article-nav">
                    
                        <div class="article-prev">
                            <a class="prev"
                               rel="prev"
                               href="/2021/07/22/MySql-row-number-%E5%BA%8F%E5%8F%B7%E5%87%BD%E6%95%B0/"
                            >
                            <span class="left arrow-icon flex-center">
                              <i class="fas fa-chevron-left"></i>
                            </span>
                                <span class="title flex-center">
                                <span class="post-nav-title-item">MySql row_number()序号函数</span>
                                <span class="post-nav-item">Prev posts</span>
                            </span>
                            </a>
                        </div>
                    
                    
                        <div class="article-next">
                            <a class="next"
                               rel="next"
                               href="/2021/06/30/%E4%BF%A1%E4%BB%B0/"
                            >
                            <span class="title flex-center">
                                <span class="post-nav-title-item">念想</span>
                                <span class="post-nav-item">Next posts</span>
                            </span>
                                <span class="right arrow-icon flex-center">
                              <i class="fas fa-chevron-right"></i>
                            </span>
                            </a>
                        </div>
                    
                </div>
            

            
                <div class="comment-container">
                    
<div class="comments-container">
    <div id="comments-anchor"></div>
    <div class="comment-area-title">
        <i class="fas fa-comments"></i>&nbsp;Comments
    </div>
    
        
            

    <div class="valine-container">
        <script data-pjax src="//cdn.jsdelivr.net/npm/valine@latest/dist/Valine.min.js"></script>
        <div id="vcomments"></div>
        <script data-pjax>
          function loadValine() {
            new Valine({
              el: '#vcomments',
              appId: 'v7SvjMu6zFX3LPmiYGos39qx-gzGzoHsz',
              appKey: 'MlLOMElKkabjL3SHiqXhFWAW',
              meta: ['nick', 'mail', 'link'],
              avatar: 'wavatar',
              enableQQ: true,
              placeholder: '',
              lang: 'en'.toLowerCase()
            });

            function getAuthor(language) {
              switch (language) {
                case 'en':
                  return 'Author';
                case 'zh-CN':
                  return '博主';
                default:
                  return 'Master';
              }
            }

            // Add "Author" identify
            const getValineDomTimer = setInterval(() => {
              const vcards = document.querySelectorAll('#vcomments .vcards .vcard');
              if (vcards.length > 0) {
                let author = 'anlondon';

                if (author) {
                  for (let vcard of vcards) {
                    const vnick_dom = vcard.querySelector('.vhead .vnick');
                    const vnick = vnick_dom.innerHTML;
                    if (vnick === author) {
                      vnick_dom.innerHTML = `${vnick} <span class="author">${getAuthor(KEEP.hexo_config.language)}</span>`
                    }
                  }
                }
                clearInterval(getValineDomTimer);
              } else {
                clearInterval(getValineDomTimer);
              }
            }, 2000);
          }

          if ('true' === 'true') {
            const loadValineTimeout = setTimeout(() => {
              loadValine();
              clearTimeout(loadValineTimeout);
            }, 1000);
          } else {
            window.addEventListener('DOMContentLoaded', loadValine);
          }
        </script>
    </div>



        
    
</div>

                </div>
            
        </div>

        
            <div class="toc-content-container">
                <div class="post-toc-wrap">
    <div class="post-toc">
        <ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%89%8D%E8%A8%80"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%83%A8%E7%BD%B2"><span class="nav-number">2.</span> <span class="nav-text">部署</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E4%B8%80%EF%BC%8C%E9%AA%8C%E8%AF%81token"><span class="nav-number">2.0.1.</span> <span class="nav-text">一，验证token</span></a></li></ol></li></ol></li></ol>
    </div>
</div>

            </div>
        
    </div>
</div>


                
            </div>

        </div>

        <div class="page-main-content-bottom">
            
<footer class="footer">
    <div class="info-container">
        <div class="copyright-info info-item">
            &copy;
            
                <span>2020</span> -
            
            2023
            
                &nbsp;<i class="fas fa-heart icon-animate"></i>
                &nbsp;<a href="/">anlondon</a>
            
        </div>
        
            <script async data-pjax
                    src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <div class="website-count info-item">
                
                    Visitor Count&nbsp;<span id="busuanzi_value_site_uv"></span>&ensp;
                
                
                    Totalview&nbsp;<span id="busuanzi_value_site_pv"></span>
                
            </div>
        
        <div class="theme-info info-item">
            Powered by <a target="_blank" href="https://hexo.io">Hexo</a>&nbsp;|&nbsp;Theme&nbsp;<a class="theme-version" target="_blank" href="https://github.com/XPoet/hexo-theme-keep">Keep v3.5.2</a>
        </div>
        
            <div class="icp-info info-item">
                <a target="_blank" rel="nofollow"
                   href="https://beian.miit.gov.cn"
                >
                    蜀ICP备2021002582号
                </a>

                <a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=51010602001866" style="display:inline-block;text-decoration:none;height:17px;line-height:17px;">
                    <img src="https://www.beian.gov.cn/img/new/gongan.png" style="float:left;"/>
                    <p style="float:left;height:20px;line-height:20px;margin: 0px 0px 0px 5px;">川公网安备 51010602001866号</p>
                </a>

            </div>
        
        
    </div>
</footer>

        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="tools-list">
        <!-- TOC aside toggle -->
        
            <li class="tools-item flex-center toggle-show-toc">
                <i class="fas fa-list"></i>
            </li>
        

        <!-- go comment -->
        
            <li class="tools-item flex-center go-to-comments">
                <i class="fas fa-comment"></i>
                <span class="post-comments-count"></span>
            </li>
        
    </ul>
</div>

        </div>
    

    <div class="right-bottom-side-tools">
        <div class="side-tools-container">
    <ul class="side-tools-list">
        <li class="tools-item tool-font-adjust-plus flex-center">
            <i class="fas fa-search-plus"></i>
        </li>

        <li class="tools-item tool-font-adjust-minus flex-center">
            <i class="fas fa-search-minus"></i>
        </li>

        <li class="tools-item tool-dark-light-toggle flex-center">
            <i class="fas fa-moon"></i>
        </li>

        <!-- rss -->
        

        
            <li class="tools-item tool-scroll-to-top flex-center">
                <i class="fas fa-arrow-up"></i>
            </li>
        

        <li class="tools-item tool-scroll-to-bottom flex-center">
            <i class="fas fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="exposed-tools-list">
        <li class="tools-item tool-toggle-show flex-center">
            <i class="fas fa-cog fa-spin"></i>
        </li>
        
    </ul>
</div>

    </div>

    <div class="zoom-in-image-mask">
    <img class="zoom-in-image">
</div>


    
        <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
          <span class="search-input-field-pre">
            <i class="fas fa-keyboard"></i>
          </span>
            <div class="search-input-container">
                <input autocomplete="off"
                       autocorrect="off"
                       autocapitalize="off"
                       placeholder="Search..."
                       spellcheck="false"
                       type="search"
                       class="search-input"
                >
            </div>
            <span class="close-popup-btn">
                <i class="fas fa-times"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fas fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>

    

</main>




<script src="/js/utils.js"></script>

<script src="/js/main.js"></script>

<script src="/js/header-shrink.js"></script>

<script src="/js/back2top.js"></script>

<script src="/js/dark-light-toggle.js"></script>





    
<script src="/js/local-search.js"></script>




    
<script src="/js/code-block.js"></script>




    
<script src="/js/lazyload.js"></script>



<div class="post-scripts pjax">
    
        
<script src="/js/post-helper.js"></script>

        
            
<script src="/js/libs/anime.min.js"></script>

        
        
            
<script src="/js/toc.js"></script>

        
    
</div>


    
<script src="/js/libs/pjax.min.js"></script>

<script>
    window.addEventListener('DOMContentLoaded', () => {
        window.pjax = new Pjax({
            selectors: [
                'head title',
                '.page-container',
                '.pjax'
            ],
            history: true,
            debug: false,
            cacheBust: false,
            timeout: 0,
            analytics: false,
            currentUrlFullReload: false,
            scrollRestoration: false,
            // scrollTo: true,
        });

        document.addEventListener('pjax:send', () => {
            KEEP.utils.pjaxProgressBarStart();
        });

        document.addEventListener('pjax:complete', () => {
            KEEP.utils.pjaxProgressBarEnd();
            window.pjax.executeScripts(document.querySelectorAll('script[data-pjax], .pjax script'));
            KEEP.refresh();
        });
    });
</script>



</body>
</html>
